package com.cdu.xinmiao.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cdu.xinmiao.R;
import com.cdu.xinmiao.adapter.StudentAdapter;
import com.cdu.xinmiao.data.Student;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.RadarData;
import com.github.mikephil.charting.data.RadarDataSet;
import com.github.mikephil.charting.data.RadarEntry;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.xuexiang.xui.widget.actionbar.TitleBar;

import java.util.ArrayList;
import java.util.List;

public class RankActivity extends AppCompatActivity {
    private TitleBar titleBar;
    private BottomNavigationView bottomNavigationView;
    private RadarChart radarChart;
    private RecyclerView recyclerView;
    private StudentAdapter studentAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_rank);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.rank), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        getId();

        loadData();

        bindClick();
    }

    private void bindClick() {
        titleBar.setLeftClickListener(v -> finish());

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.item_home:
                        Intent intent1 = new Intent(RankActivity.this, HomeActivity.class);
                        startActivity(intent1);
                        break;
                    case R.id.item_user:
                        Intent intent2 = new Intent(RankActivity.this, UserActivity.class);
                        startActivity(intent2);
                        break;
                    case R.id.item_mission:
                        Intent intent3 = new Intent(RankActivity.this, MissionActivity.class);
                        startActivity(intent3);
                        break;
                    case R.id.item_rank:
                        break;
                }
                return false;
            }
        });
    }

    private void loadData() {
        // 生成10个学院的随机完成进度及对应的学院名称
        List<RadarEntry> entries = new ArrayList<>();
        List<String> labels = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            float progress = (float) (Math.random() * 100); // 随机生成0到100的进度值
            entries.add(new RadarEntry(progress));
            labels.add("学院" + (i + 1)); // 添加学院名称
        }

        RadarDataSet dataSet = new RadarDataSet(entries, "各学院学生任务完成度百分比");
        dataSet.setColor(Color.parseColor("#FF6655"));
        dataSet.setFillColor(Color.parseColor("#FF9593"));
        dataSet.setDrawFilled(true);
        dataSet.setFillAlpha(80);
        dataSet.setLineWidth(3f);
        dataSet.setValueTextColor(Color.parseColor("#FF9593"));
        dataSet.setValueTextSize(12f);

        RadarData radarData = new RadarData(dataSet);

        // 设置每个数据点的标签
        XAxis xAxis = radarChart.getXAxis();
        xAxis.setValueFormatter(new IndexAxisValueFormatter(labels));

        // 设置雷达图的最大值为100
        YAxis yAxis = radarChart.getYAxis();
        yAxis.setAxisMaximum(80);

        radarChart.setData(radarData);

        radarChart.getDescription().setEnabled(false); // 禁用描述
        radarChart.setWebLineWidth(1f);
        radarChart.setWebColor(Color.GRAY);
        radarChart.setWebLineWidthInner(1f);
        radarChart.setWebColorInner(Color.GRAY);
        radarChart.setWebAlpha(100);

        radarChart.invalidate(); // 刷新图表

        // 学生积分排行榜
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        List<Student> studentList = generateStudentData(); // 生成学生数据
        studentAdapter = new StudentAdapter(studentList);
        recyclerView.setAdapter(studentAdapter);
    }

    private void getId() {
        titleBar = findViewById(R.id.titleBar_back);
        radarChart = findViewById(R.id.radar_chart);
        recyclerView = findViewById(R.id.recycler_view);
        bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setSelectedItemId(R.id.item_rank);
    }

    private List<Student> generateStudentData() {
        List<Student> studentList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            studentList.add(new Student("Student " + (i + 1), i + 1, (10 - i) * 50)); // 生成10个学生的信息
        }
        return studentList;
    }
}